from .setting import *
from .verify import *
from django.shortcuts import render
import requests
# Create your views here.


@verify_control
def asser_buyer(request):
    """
        @author:薛威宁
        @date:2018/10/20/
        提交方式 ：POST
        提交数据格式 ：{提交数据库格式:{"asset_name"："xxx","pur_name"："x",user"："xx","save_room"："仓库"}
        仓库格式:{'asset_name':'xxx','room':'xxx','asset_num':'数字'}
    """

    info = dict(request.POST)
    print(info)
    asset_url = url_ip + "apiasset/asset_buy/"
    r = requests.post(asset_url, data=info)
    if "资产已入库" == r.json()["message"]:
        context = {"message": r.json()["message"]}
        return render(request, "asset/asset_by.html", context)
    else:
        context = {"message": "购买成功"}
        return render(request, "asset/asset_by.html", context)


@verify_control
def add_asset(request):
    """
        @author: 董海明
        @date: 10/25/2018
        为add_asset_api提供数据
        添加资产：add_asset_api
        提交方法：requests.post
                提交数据格式：{"asset":"xxx", "price":"xxx"}
    """
    info = dict(request.POST)
    if info["asset"][0] == "" or info["price"][0] == "":
        context = {}
        return render(request, "asset/add_asset_page.html", context)
    asset_url = url_ip + "apiasset/add_asset_api/"
    r = requests.post(asset_url, data={"asset": info["asset"][0], "price": info["price"][0]})
    if "资产添加成功" == r.json()["message"]:
        context = {}
        return render(request, "asset/add_asset_page.html", context)
    else:
        context = {"message": r.json()["message"]}
        return render(request, "asset/add_asset_page.html", context)


@verify_control
def del_asset(request):
    """
        @author: 董海明
        @date: 10/25/2018
        为del_asset_api接口提交数据
        提交方法：requests.post
        提交数据格式{"asset":"xxx"}
    """
    info = dict(request.POST)
    if info["asset"][0] == "":
        context = {}
        return render(request, "asset/del_asset.html", context)
    asset_url = url_ip + "apiasset/del_asset_api/"
    r = requests.post(asset_url, data={"asset": info["asset"][0]})
    if "资产删除成功" == r.json()["message"]:
        context = {}
        return render(request, "asset/del_asset.html", context)
    else:
        context = {"message": r.json()["message"]}
        return render(request, "asset/del_asset.html", context)


@verify_control
def find_asset(request):
    """
        @author: 董海明
        @date: 10/25/2018
        为find_asset_api接口提交数据
        使用方法：requests.post
        提交数据：{"tower_name":"xxx", "floor_name":"xxx", "room_name":"xxx"}
        根据提供的数据查找所在房间内的所有资产明细
    """
    info = dict(request.POST)
    if info["tower_name"][0] == "" or info["floor_name"][0] == "" or info["room_name"][0] == "":
        context = {}
        return render(request, "asset/find_asset.html", context)
    asset_url = url_ip + "apiasset/find_asset_api/"
    r = requests.post(asset_url, data={"tower_name": info["tower_name"][0],
                                       "floor_name": info["floor_name"][0],
                                       "room_name": info["room_name"][0]})
    if "该房间内有以下商品" in r.json()["message"]:
        context = {}
        return render(request, "asset/find_asset.html", context)
    else:
        context = {}
        return render(request, "asset/find_asset.html", context)


@verify_control
def change_asset(request):
    """
        功能:修改资产名
        author:成晨
        date:18/10/26
        提交数据格式：{"asset":"xxx", "new_asset":"xxx"}
        使用方法：用requests.post提交数据
    """
    info = dict(request.POST)
    if info["asset"][0] == "" or info["new_asset"][0] == "":
        context = {"message": "内容不能为空"}
        return render(request, "asset/change_asset.html", context)
    else:

        asset_url = url_ip + "apiasset/change_asset_api/"
        r = requests.post(asset_url, data={"asset": info["asset"][0], "new_asset": info["new_asset"][0]})
        if "修改成功" == r.json()["message"]:
            context = {"message": r.json()["message"]}
            return render(request, "asset/change_asset.html", context)
        else:
            context = {}
            return render(render, "asset/change_asset.html", context)


@verify_control
def change_price(request):
    """
        功能:修改价格名
        author:成晨
        date:18/10/26
        提交数据格式：{"asset":"xxx", "new_price":"xxx"}
        使用方法：
                使用post方法提交数据到本接口的链接地址，不可以使用get
    """
    info = dict(request.POST)

    if info["asset"][0] == "" or info["new_price"][0] == "":
        context = {"message": "内容不能为空"}
        return render(request, "asset/change_price.html", context)
    else:

        asset_url = url_ip + "apiasset/change_price_api/"
        r = requests.post(asset_url, data={"asset": info["asset"][0], "new_price": info["new_price"][0]})
        if "修改成功" == r.json()["message'"]:
            context = {'message': r.json()["message"]}
            return render(request, "asset/change_price.html", context)
        else:
            context = {}
            return render(render, "asset/change_price.html", context)


@verify_control
def asset_move(request):
    """
        功能:资产转移
        author: 吕琛琛
        date: 20218/10/25
        为asset_move_api接口提交数据
        提交方式：POST
        提交数据格式：{'asset_name': 'xxx'}
        使用方法：
                使用post方法提交数据到本接口的链接地址，不可以使用get
    """
    info = dict(request.POST)
    tower_name = info['submit_tower_name'][0]
    floor_name = info['submit_floor_num'][0]
    room_name = info['submit_room_num'][0]
    asset = info['asset_name'][0]
    asset_num = info['asset_num'][0]
    tower_name1 = info['receive_tower_name'][0]
    floor_name1 = info['receive_floor_num'][0]
    room_name1 = info['receive_room_num'][0]
    asset_url = url_ip + 'apiasset/asset_move_api/'
    data = {'tower_name': tower_name, 'floor_name': floor_name, 'room_name': room_name, 'asset': asset, 'asset_num': asset_num, 'tower_name1': tower_name1, 'floor_name1': floor_name1, 'room_name1': room_name1}
    r = requests.post(asset_url, data=data)
    rj = r.json()['message']
    if rj == '转移成功':
        context = {'message': '转移成功'}
        return render(request, 'asset/asset_move.html', context)
    else:
        context = {'message': rj}
        return render(request, 'asset/asset_move.html', context)


def asset_show(request):
    '''
    资产展示
    @author:'郭峰'
    @date: 2018/10/24
    渲染html的格式是: {
                    '序号':forloop.counter,
                    '资产名':x.asset.asset_name,
                    '数量':x.asset_num,
                    '房间':x.room.room_num,
                    '用户名':x.room.user.user_name}
    '''
    show = Interchange.objects.all()
    return render(render, "asset/assetshow.html")


def home(request):
    """
        功能：主页
        author: 吕琛琛
        date: 20218/10/25
    """
    return render(request, "asset/home.html")


def add_asset_page(request):
    """
        功能：添加资产页面
        author: 吕琛琛
        date: 20218/10/25
    """
    return render(request, "asset/add_asset_page.html")


def asset_move_page(request):
    """
        功能：资产移动页面
        author: 吕琛琛
        date: 20218/10/25
    """
    return render(request, "asset/asset_move.html")






